home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / snip9_91.arc / FLN_FIX.C < prev    next >
C/C++ Source or Header  |  1991-09-17  |  4KB  |  136 lines

  1. /*
  2. **  FLN_FIX.C
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <dos.h>
  8. #include <io.h>
  9.  
  10. #define LAST_CHAR(string) (((char *)string)[strlen(string)-1])
  11.  
  12. typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
  13.  
  14. char *unix2dos(char *path);
  15.  
  16. /****************************************************************/
  17. /*                                                              */
  18. /*  Function to `crunch' dot directories and check for          */
  19. /*  DOS-valid path strings. Drive specifiers in the path        */
  20. /*  ignored.                                                    */
  21. /*                                                              */
  22. /****************************************************************/
  23.  
  24. char *fln_fix(char *path)
  25. {
  26.       LOGICAL dir_flag = FALSE, root_flag = FALSE;
  27.       char *r, *p, *q, *s;
  28.  
  29.       if (path)
  30.             strupr(path);
  31.  
  32.       /* Ignore leading drive specs   */
  33.  
  34.       if (NULL == (r = strrchr(path, ':')))
  35.             r = path;
  36.       else  ++r;
  37.  
  38.       unix2dos(r);                      /* Convert Unix to DOS style    */
  39.  
  40.       while ('\\' == *r)                /* Ignore leading backslashes   */
  41.       {
  42.             if ('\\' == r[1])
  43.                   strcpy(r, &r[1]);
  44.             else
  45.             {
  46.                   root_flag = TRUE;
  47.                   ++r;
  48.             }
  49.       }
  50.  
  51.       p = r;                            /* Change "\\" to "\"           */
  52.       while (NULL != (p = strchr(p, '\\')))
  53.       {
  54.             if ('\\' ==  p[1])
  55.                   strcpy(p, &p[1]);
  56.             else  ++p;
  57.       }
  58.  
  59.       while ('.' == *r)                 /* Scrunch leading ".\"         */
  60.       {
  61.             if ('.' == r[1])
  62.             {
  63.                   /* Ignore leading ".."                                */
  64.  
  65.                   for (p = (r += 2); *p && (*p != '\\'); ++p)
  66.                         ;
  67.             }
  68.             else
  69.             {
  70.                   for (p = r + 1 ;*p && (*p != '\\'); ++p)
  71.                         ;
  72.             }
  73.             strcpy(r, p + ((*p) ? 1 : 0));
  74.       }
  75.  
  76.       while ('\\' == LAST_CHAR(path))   /* Strip trailing backslash     */
  77.       {
  78.             dir_flag = TRUE;
  79.             LAST_CHAR(path) = '\0';
  80.       }
  81.  
  82.       s = r;
  83.  
  84.       /* Look for "\." in path        */
  85.  
  86.       while (NULL != (p = strstr(s, "\\.")))
  87.       {
  88.             if ('.' == p[2])
  89.             {
  90.                   /* Execute this section if ".." found                 */
  91.  
  92.                   q = p - 1;
  93.                   while (q > r)           /* Backup one level           */
  94.                   {
  95.                         if (*q == '\\')
  96.                               break;
  97.                         --q;
  98.                   }
  99.                   if (q > r)
  100.                   {
  101.                         strcpy(q, p + 3);
  102.                         s = q;
  103.                   }
  104.                   else if ('.' != *q)
  105.                   {
  106.                         strcpy(q + ((*q == '\\') ? 1 : 0),
  107.                               p + 3 + ((*(p + 3)) ? 1 : 0));
  108.                         s = q;
  109.                   }
  110.                   else  s = ++p;
  111.  
  112.             }
  113.             else
  114.             {
  115.                   /* Execute this section if "." found                  */
  116.  
  117.                   q = p + 2;
  118.                   for ( ;*q && (*q != '\\'); ++q)
  119.                         ;
  120.                   strcpy (p, q);
  121.             }
  122.       }
  123.  
  124.       if (root_flag)  /* Embedded ".." could have bubbled up to root  */
  125.       {
  126.             for (p = r; *p && ('.' == *p || '\\' == *p); ++p)
  127.                   ;
  128.             if (r != p)
  129.                   strcpy(r, p);
  130.       }
  131.  
  132.       if (dir_flag)
  133.             strcat(path, "\\");
  134.       return path;
  135. }
  136.